<!--
  * 打印模板
  *
  * @Author:    lmabbe
  * @Date:      2025-06-25 12:06:27
  * @Copyright  lmabbe
-->
<template>
  <a-drawer :destroyOnClose="true" :maskClosable="false" :open="visibleFlag" :title="form.templateId ? '编辑' : '添加'" :width="450" @close="onClose">
    <a-form ref="formRef" :label-col="{ span: 5 }" :model="form" :rules="rules">
      <a-input v-model:value="form.templateId" hidden placeholder="打印模板ID" style="width: 100%" />
      <a-form-item label="打印模板名称" name="templateName">
        <a-input v-model:value="form.templateName" placeholder="打印模板名称" style="width: 100%" />
      </a-form-item>
      <a-form-item label="模板内容" name="templateJson">
        <a-textarea v-model:value="form.templateJson" :rows="25" placeholder="模板内容" style="width: 100%" />
      </a-form-item>
      <a-form-item label="纸张类型" name="paperType">
        <a-input v-model:value="form.paperType" placeholder="纸张类型" style="width: 100%" />
      </a-form-item>
      <a-form-item label="纸张宽度" name="paperWidth">
        <a-input-number v-model:value="form.paperWidth" placeholder="纸张宽度" style="width: 100%" />
      </a-form-item>
      <a-form-item label="纸张高度" name="paperHeight">
        <a-input-number v-model:value="form.paperHeight" placeholder="纸张高度" style="width: 100%" />
      </a-form-item>
    </a-form>

    <template #footer>
      <a-space>
        <a-button @click="onClose">取消</a-button>
        <a-button type="primary" @click="onSubmit">保存</a-button>
      </a-space>
    </template>
  </a-drawer>
</template>
<script setup>
  import { nextTick, reactive, ref } from 'vue';
  import _ from 'lodash';
  import { message } from 'ant-design-vue';
  import { SmartLoading } from '/@/components/framework/smart-loading';

  import { printTemplateApi } from '/@/api/business/basic/print/print-template-api.js';
  import { smartSentry } from '/@/lib/smart-sentry'; // ------------------------ 事件 ------------------------

  // ------------------------ 事件 ------------------------

  const emits = defineEmits(['reloadList']);

  // ------------------------ 显示与隐藏 ------------------------
  // 是否显示
  const visibleFlag = ref(false);

  function show(rowData) {
    Object.assign(form, formDefault);
    if (rowData && !_.isEmpty(rowData)) {
      Object.assign(form, rowData);
    }
    // 使用字典时把下面这注释修改成自己的字典字段 有多个字典字段就复制多份同理修改 不然打开表单时不显示字典初始值
    // if (form.status && form.status.length > 0) {
    //   form.status = form.status.map((e) => e.valueCode);
    // }
    visibleFlag.value = true;
    nextTick(() => {
      formRef.value.clearValidate();
    });
  }

  function onClose() {
    Object.assign(form, formDefault);
    visibleFlag.value = false;
  }

  // ------------------------ 表单 ------------------------

  // 组件ref
  const formRef = ref();

  const formDefault = {
    templateId: undefined, //打印模板ID
    templateName: undefined, //打印模板名称
    templateJson: undefined, //模板内容
    paperType: undefined, //纸张类型
    paperWidth: undefined, //纸张宽度
    paperHeight: undefined, //纸张高度
  };

  let form = reactive({ ...formDefault });

  const rules = {
    templateId: [{ required: true, message: '打印模板ID 必填' }],
    templateName: [{ required: true, message: '打印模板名称 必填' }],
    templateJson: [{ required: true, message: '模板内容 必填' }],
    paperType: [{ required: true, message: '纸张类型 必填' }],
  };

  // 点击确定，验证表单
  async function onSubmit() {
    try {
      await formRef.value.validateFields();
      save();
    } catch (err) {
      message.error('参数验证错误，请仔细填写表单数据!');
    }
  }

  // 新建、编辑API
  async function save() {
    SmartLoading.show();
    try {
      if (form.templateId) {
        await printTemplateApi.update(form);
      } else {
        await printTemplateApi.add(form);
      }
      message.success('操作成功');
      emits('reloadList');
      onClose();
    } catch (err) {
      smartSentry.captureError(err);
    } finally {
      SmartLoading.hide();
    }
  }

  defineExpose({
    show,
  });
</script>
